﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_WebOpenId_FormItem" prop="Entity.WebOpenId">
								<template #label>
									<WtmLabelTooltip label="网站OpenId" tooltip="微信网站应用OpenId" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.WebOpenId" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_MpOpenId_FormItem" prop="Entity.MpOpenId">
								<template #label>
									<WtmLabelTooltip label="公众号OpenId" tooltip="微信公众号OpenId" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.MpOpenId" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_MiniProgramOpenId_FormItem" prop="Entity.MiniProgramOpenId">
								<template #label>
									<WtmLabelTooltip label="小程序OpenId" tooltip="微信小程序OpenId" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.MiniProgramOpenId" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_UnionId_FormItem" prop="Entity.UnionId">
								<template #label>
									<WtmLabelTooltip label="UnionId" tooltip="微信开放平台统一标识" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.UnionId" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_NickName_FormItem" prop="Entity.NickName">
								<template #label>
									<WtmLabelTooltip label="昵称" tooltip="微信用户昵称" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.NickName" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_HeadImgUrl_FormItem" prop="Entity.HeadImgUrl">
								<template #label>
									<WtmLabelTooltip label="头像" tooltip="微信用户头像URL" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.HeadImgUrl" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>系统用户绑定</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_UserId_FormItem" prop="Entity.UserId">
								<template #label>
									<WtmLabelTooltip label="绑定用户" tooltip="关联的系统用户对象" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.UserId" clearable class="width100">
									<el-option v-for="item in stateData.AllFrameworkUsers" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div style="text-align: right">
					<WtmButton @click="onSubmit" type="primary" :button-text="$t('message._system.common.vm.submit')" style="margin-top: 15px" />
					<WtmButton @click="onClose" type="default" :button-text="$t('message._system.common.vm.cancel')" style="margin-top: 15px" />
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.add;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted } from 'vue';
import { SysWechatUserApi as api } from '/@/views/wechat/syswechatuser/api';
import other from '/@/utils/other';
import { ISysWechatUserVm } from '/@/views/wechat/syswechatuser/api/interface';
import { IViewModel } from '/@/types/common';

const ci = getCurrentInstance() as any;
const loading = ref(false);
// 定义变量内容
const formRef = ref();
const stateData = reactive<IViewModel<ISysWechatUserVm>>({
	vmModel: {
		Entity: {
			WebOpenId: null,
			MpOpenId: null,
			MiniProgramOpenId: null,
			UnionId: null,
			NickName: null,
			HeadImgUrl: null,
			UserId: null,
			UserName: null,
		},
	},

	AllFrameworkUsers: [] as any[],
});

// 取消
const onClose = () => {
	closeDialog();
};

// 提交
const onSubmit = () => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			api()
				.add(stateData.vmModel)
				.then(() => {
					ElMessage.success(ci.proxy.$t('message._system.common.vm.submittip'));
					emit('refresh');
					closeDialog();
				})
				.catch((error) => {
					other.setFormError(ci, error);
				})
				.finally(() => {
					loading.value = false;
				});
		}
	});
};

// 页面加载时
onMounted(() => {
	loading.value = true;
	if (ci.attrs['wtmdata']) {
		stateData.vmModel.Entity.ID = ci.attrs['wtmdata'].ID;
	}
	const promises: Promise<any>[] = [
		other.getSelectList('/api/SysWechatUser/GetFrameworkUsers', [], false).then((x) => {
			stateData.AllFrameworkUsers = x;
		}),
	];
	Promise.allSettled(promises).finally(() => {
		loading.value = false;
	});
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog']);
// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};
</script>
